<extend name="Public/base" />
<block name="body">
<style type="text/css">
	.alert{
		padding: 20px;
	}
</style>
	<!-- 主体 -->
	<div id="main" class="main" style="min-height: 760px;">

		<div class="well">
			<script>
			var h = document.documentElement.clientHeight;
			$(".gw-container").css('min-height',h);
			</script>

			<div class="clearfix">
				<div class="alert alert-warning">
					<p>正在更新系统文件, 请不要关闭窗口。</p>
					<p>如果下载文件失败，可能造成的原因：<font color="red">写入失败</font>，请仔细检查写入权限是否正确。</p>
				</div>
				<div class="alert alert-info form-horizontal ng-scope" ng-controller="processor">
					<dl class="dl-horizontal">
						<dt style="font-size:18px;font-weight:700;color:#000;margin:10px 0px;">整体进度</dt>
						<dd class="ng-binding"><span id="i"></span>/<span id="total"></span></dd>
						<dt style="font-size:18px;font-weight:700;color:#000;margin:10px 0px;">正在下载文件</dt>
						<dd class="ng-binding"><span id="path"></span></dd>
					</dl>
					<dl class="dl-horizontal download_failed">
						<dt style="font-size:18px;font-weight:700;color:#000;margin:10px 0px;">文件状态（绿色表示下载成功，红色表示下载失败）</dt>
					</dl>
					<button id="retry" class="btn submit-btn ajax-post" type="submit" target-form="form-horizontal" style="display:none">重 试</button>
				</div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
	$(function() {
		var version_id = "{$version_id}";
		var files = "{$files}";
		var files = files.split(",");
		var total = files.length;
		$("#total").html(total);
		$("#i").html(0);

		upgrade(files,version_id);
	});

	function upgrade(uplist,version_id) {
		var failLists = [];
        var successLists = [];
		$.each(uplist, function(index, path) {
			$("#path").html(path);
			$.ajax({
				url : "{:U('do_upgrade', '', '')}",
				type : "post",
				dataType : "json",
				data : {
					'file_path' : path,
					'version_id' : version_id
				},
				success : function(data) {
					console.log(data);
					if (data.errcode == 1111) {
						successLists.push(path);
						$('.download_failed').append('<dd><font color="green">'+path+'</font></dd>');
					} else {
						failLists.push(path);
						$('.download_failed').append('<dd><font color="red">'+path+'</font></dd>');
					};

					var complete = parseInt($("#i").html())+1;
					$("#i").html(complete);
					$("#path").html('<dd>'+path+'</dd>');

					if (complete == uplist.length) {
				
						if (failLists.length > 0) {
							$(document).data('failLists',failLists);
							$("#retry").css("display", "block");
							
						} else {
							
							alert("更新完成");
							$.ajax({
								url : "{:U('upgrade_version_log')}",
								type : "post",
								dataType : "json",
								data : {
									version_id : version_id
								},
								success : function(data) {
									window.location.href = "{:U('upgrade')}";
								}
							})
							
						}

						return false;
					}
				},

				error : function(data) {
					alert('发生错误，更新中止');
				}
			});
		});

		if (failLists.leng==0) {
			location.href = "{:U('upgrade')}";
		}

		else {
			$(document).data('failLists',failLists);
		}
	}

	$('#retry').click(function() {
		$('.download_failed dd').remove();
		var failLists = $(document).data('failLists');
		$("#total").html(failLists.length);
		$("#i").html(0);
		upgrade(failLists,"{$version_id}");
	});
	</script>
</block>